package com.google.code.mochaccino.framework.transaction.provider;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import bitronix.tm.resource.common.ResourceBean;
import java.util.concurrent.TimeUnit;
import javax.inject.Provider;

/** Resource Bean Provider Abstract Implementation for both XADataSource and XAConnectionFactory. */
public abstract class XAResourceProvider<T extends ResourceBean, Y> implements Provider<Y> {

	private String  uniqueName                 = null;
	private boolean allowLocalTransactions     = false;
	private boolean applyTransactionTimeout    = true;
	private boolean automaticEnlistingEnabled  = true;
	private boolean deferConnectionRelease     = true;
	private boolean shareAccessibleConnections = false;
	private boolean useTmJoin                  = true;
	private int     acquireIncrement           = 5;
	private int     acquisitionInterval        = (int) TimeUnit.SECONDS.toSeconds( 10 );
	private int     acquisitionTimeout         = (int) TimeUnit.SECONDS.toSeconds( 10 );
	private int     maxIdleTime                = (int) TimeUnit.MINUTES.toSeconds( 1 );
	private int     maxPoolSize                = 100;
	private int     minPoolSize                = 30;

	/** Constructor */
	protected XAResourceProvider( String uniqueName ) {
		this.uniqueName = uniqueName;
	}

	public T createConfiguredResource() {
		T bean = createUnconfiguredResource();
		bean.setAcquireIncrement( acquireIncrement );
		bean.setAcquisitionInterval( acquisitionInterval );
		bean.setAcquisitionTimeout( acquisitionTimeout );
		bean.setAllowLocalTransactions( allowLocalTransactions );
		bean.setApplyTransactionTimeout( applyTransactionTimeout );
		bean.setAutomaticEnlistingEnabled( automaticEnlistingEnabled );
		bean.setDeferConnectionRelease( deferConnectionRelease );
		bean.setMaxIdleTime( maxIdleTime );
		bean.setMaxPoolSize( maxPoolSize );
		bean.setMinPoolSize( minPoolSize );
		bean.setShareTransactionConnections( shareAccessibleConnections );
		bean.setUniqueName( uniqueName );
		bean.setUseTmJoin( Boolean.valueOf( useTmJoin ) );
		return bean;
	}

	protected abstract T createUnconfiguredResource();

	public void setAcquireIncrement( int acquireIncrement ) {
		this.acquireIncrement = acquireIncrement;
	}

	public void setAcquisitionInterval( int acquisitionInterval ) {
		this.acquisitionInterval = acquisitionInterval;
	}

	public void setAcquisitionTimeout( int acquisitionTimeout ) {
		this.acquisitionTimeout = acquisitionTimeout;
	}

	public void setAllowLocalTransactions( boolean allowLocalTransactions ) {
		this.allowLocalTransactions = allowLocalTransactions;
	}

	public void setApplyTransactionTimeout( boolean applyTransactionTimeout ) {
		this.applyTransactionTimeout = applyTransactionTimeout;
	}

	public void setAutomaticEnlistingEnabled( boolean automaticEnlistingEnabled ) {
		this.automaticEnlistingEnabled = automaticEnlistingEnabled;
	}

	public void setDeferConnectionRelease( boolean deferConnectionRelease ) {
		this.deferConnectionRelease = deferConnectionRelease;
	}

	public void setMaxIdleTime( int maxIdleTime ) {
		this.maxIdleTime = maxIdleTime;
	}

	public void setMaxPoolSize( int maxPoolSize ) {
		this.maxPoolSize = maxPoolSize;
	}

	public void setMinPoolSize( int minPoolSize ) {
		this.minPoolSize = minPoolSize;
	}

	public void setShareAccessibleConnections( boolean shareAccessibleConnections ) {
		this.shareAccessibleConnections = shareAccessibleConnections;
	}

	public void setUseTmJoin( boolean useTmJoin ) {
		this.useTmJoin = useTmJoin;
	}
}
